package sg.nus.iss.vmcs.dao;

import sg.nus.iss.vmcs.VMCSException;
import sg.nus.iss.vmcs.models.Coin;
import sg.nus.iss.vmcs.models.CoinRegistry;
import sg.nus.iss.vmcs.utils.Environment;

/**
 * Coin Registry DAO using property files 
 * @author Seng Chia How
 */
public class PropertyCoinRegistryDAO implements CoinRegistryDAO {
	
	private CashPropertyLoader cpp;
	
	public PropertyCoinRegistryDAO() throws VMCSException {
		try{
			cpp = new CashPropertyLoader(Environment.getInstance().getCashPropFile());
			cpp.initialize();
		}
		catch(VMCSException e) {
			throw new VMCSException ("vmcs.dao.PropertyCoinRegistryDAO" , "Error loading Property Files: " + e);
		}
	}
	
	/**
	 * Gets a coin registry to hold the different types of coins
	 */
	@Override
	public CoinRegistry getCoinRegistry() throws VMCSException {
		try {
			CoinRegistry coinReg = CoinRegistry.getInstance();
			
			for(int i = 0; i < cpp.getNumOfItems(); i++) {
				Coin coin = (Coin)cpp.getItem(i + 1).getContent();
				if(coin != null) coinReg.addCoin(coin);
			}
			
			return coinReg;
		}
		catch (Exception e){
			throw new VMCSException ("vmcs.dao.PropertyCoinRegistryDAO" , "Error loading Coin Registry: " + e);
		}
	}
}
